package com.hpjz.basicofing.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.hpjz.basicofing.bean.OrganizationBean;
import com.hpjz.basicofing.dao.IOrganizationDao;
import com.hpjz.basicofing.service.IOrganizationService;
import com.hpjz.basicofing.web.vo.OrganizationVO;
import com.hpjz.common.utils.DateUtil;

@Service("organizationService")
public class OrganizationServiceImpl implements IOrganizationService{
	
	/**
	 * 组织机构 dao
	 */
	@Resource
	private IOrganizationDao organizationDao;

	@Override
	public boolean insertOrganization(OrganizationVO vo) {
		OrganizationBean bean = this.voToBean(vo);
		bean.setCreatetime(new Date());
		bean.setUpdatetime(new Date());
		bean.setStatus("1");
		boolean isInsert = this.organizationDao.insert(bean) > 0;
		return isInsert;
	}

	

	@Override
	public boolean updateOrganization(OrganizationVO vo) {
		OrganizationBean bean = this.voToBean(vo);
		bean.setUpdatetime(new Date());
		boolean isUpdate = this.organizationDao.update(bean) > 0;
		return isUpdate;
	}

	@Override
	public boolean deleteOrganization(OrganizationVO vo) {
		OrganizationBean bean = this.voToBean(vo);
		boolean isDelete = this.organizationDao.delete(bean) > 0;
		return isDelete;
	}

	@Override
	public OrganizationVO findOrganization(OrganizationVO vo) {
		OrganizationBean bean = this.voToBean(vo);
		OrganizationBean result = this.organizationDao.findOne(bean);
		OrganizationVO resultVO = this.beanToVO(result);
		
		return resultVO;
	}


     /**
      * 将bean转换为vo
      * @param result
      * @return
      */
	private OrganizationVO beanToVO(OrganizationBean result) {
		OrganizationVO resultVO = new OrganizationVO();
		BeanUtils.copyProperties(result, resultVO);
		Date createTime = result.getCreatetime();
		Date updateTime = result.getUpdatetime();
		resultVO.setOr_id(String.valueOf(result.getOr_id()));
		if (createTime!=null) {
			resultVO.setCreatetime(DateUtil.dateToStr(createTime));
		}
		if (updateTime!=null) {
		    resultVO.setUpdatetime(DateUtil.dateToStr(updateTime));	
		}
		return resultVO;
	}
	
	
	 /**
	  * 将vo转化为Bean
	  * @param vo
	  * @return
	  */
	private OrganizationBean voToBean(OrganizationVO vo) {
		OrganizationBean bean = new OrganizationBean();
		BeanUtils.copyProperties(vo, bean);
		String createTime = vo.getCreatetime();
		String updateTime = vo.getUpdatetime();
		String or_id = vo.getOr_id();
		if (StringUtils.isNotBlank(or_id)) {
			bean.setOr_id(Long.valueOf(or_id));
		}
		if (StringUtils.isNotBlank(createTime)) {
			bean.setCreatetime(DateUtil.parse(createTime));
		}
		if (StringUtils.isNotBlank(updateTime)) {
			bean.setUpdatetime(DateUtil.parse(updateTime));
		}
		return bean;
	}

   /**
    * 查询所有医院信息
    */
	
	//TODO:查询的时候带上用户信息

	@Override
	public List<OrganizationVO> findAllOrgs() {
		
		List<OrganizationBean> orgbeans = this.organizationDao.findAll();
		//将beans转化为vos
	    List<OrganizationVO> orgvos = new ArrayList<>();
	    for (OrganizationBean bean : orgbeans) {
			OrganizationVO vo = this.beanToVO(bean);
			orgvos.add(vo);
		}    
	
		return orgvos;
	}

	
	
	
	
}
